home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / maestro / source / cdedit / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-15  |  13.0 KB  |  418 lines

  1. /*
  2.  * Copyright (c) 1990, 1991 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and 
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name
  8.  * Stanford may not be used in any advertising or publicity relating to
  9.  * the software without the specific, prior written permission of
  10.  * Stanford.
  11.  * 
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  13.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  14.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  15.  *
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  17.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
  18.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT
  19.  * ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY,
  20.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21.  * SOFTWARE.
  22.  */
  23.  
  24. /* $Header: /Source/Media/collab/cdEdit/RCS/file.c,v 2.11 92/07/17 16:33:17 drapeau Exp $ */
  25. /* $Log:    file.c,v $
  26.  * Revision 2.11  92/07/17  16:33:17  drapeau
  27.  * Minor cosmetic changes, comment changes.
  28.  * 
  29.  * Revision 2.10  92/01/03  17:49:14  drapeau
  30.  * Modified all calls to Browse() to use "0" instead of "NULL", in order to
  31.  * accommodate ANSI-C definition of NULL as (void*) 0.
  32.  * 
  33.  * Revision 2.0  91/10/06  21:01:22  chua
  34.  * Update to version 2.0
  35.  * 
  36.  * Revision 1.14  91/09/18  17:26:49  chua
  37.  * Removed the hard path for the include files.
  38.  * 
  39.  * Revision 1.13  91/09/10  13:35:12  chua
  40.  * Cast tempfile to char * in line 71
  41.  * 
  42.  * Revision 1.12  91/09/04  11:36:04  chua
  43.  * In OpenHandler, delete the stbuf structure (not used).
  44.  * 
  45.  * In SaveHandler (line 361), return Error instead of just a return.
  46.  * 
  47.  * Revision 1.11  91/09/03  15:10:47  chua
  48.  * Added the copyright header.
  49.  * 
  50.  * Deleted the functions FileError and CheckFilename, which are no longer necessary,
  51.  * since error checks for files are now done in the browser.
  52.  * 
  53.  * Include the handlers for the menu items in the Document menu (Open, Save, Save as, 
  54.  * Close).
  55.  * 
  56.  * Uses the file browser to open and save files.
  57.  * 
  58.  * Changed the file format for a cdEdit document.  It is now more readable (with more
  59.  * labelling and spacing), and also stores the total tracks and total time of a CD as a 
  60.  * form of ID.  The OpenHandler will not open a document that is incompatible with the
  61.  * currently inserted disc (the IDs don't match).
  62.  * 
  63.  * Revision 1.1  91/07/09  16:47:19  chua
  64.  * 
  65.  * 
  66.  * Revision 1.0  91/07/08  13:45:53  chua
  67.  * Initial revision
  68.  *  */
  69.  
  70. static char filercsid[] = "$Header: /Source/Media/collab/cdEdit/RCS/file.c,v 2.11 92/07/17 16:33:17 drapeau Exp $";
  71.  
  72. #include "main.h"
  73. #include <Browse.h>
  74.  
  75. int editmin, editsec;                            /* Total time for a disc read from an edit document */
  76. int editTotalTracks;                            /* Total tracks for a disc read from an edit document */
  77.  
  78. /*
  79.  * Checks if the filename specified by the user is 'untitled'.  This is not allowed as it is the default used by the timeline if no filename
  80.  * has been specified.  A return value of 1 indicates that the filename is 'untitled' and the user should choose another.  Otherwise,
  81.  * a value of 0 is returned.
  82.  */
  83. int CheckforUntitled(filename)
  84.      char *filename;
  85. {
  86.   char *tempfile;
  87.     
  88.   tempfile = (char *) rindex(filename, '/');                /* check if filename is 'untitled'.  If so, ask for another filename */
  89.   if (tempfile != NULL) 
  90.   {
  91.     if (strcmp(&tempfile[1], "untitled") == 0) 
  92.     {
  93.       return OK;
  94.     }
  95.   }  
  96.   else if (strcmp(filename, "untitled") == 0) 
  97.   {
  98.     return OK;
  99.   }
  100.   return Error;
  101. }
  102.  
  103. /*
  104.  * Menu handler for `DocumentMenu (Open)'.
  105.  */
  106. Menu_item OpenFileHandler(item, op)
  107.      Menu_item    item;
  108.      Menu_generate    op;
  109. {
  110.   switch (op) 
  111.   {
  112.    case MENU_DISPLAY:
  113.     break;
  114.    case MENU_DISPLAY_DONE:
  115.     break;
  116.    case MENU_NOTIFY:
  117.     Browse(NULL, BrowseOpen, 0, "#CD Edit Document#", "cdEdit");
  118.     break;
  119.    case MENU_NOTIFY_DONE:
  120.     break;
  121.   }
  122.   return item;
  123. }
  124.  
  125. /*
  126.  * Menu handler for `DocumentMenu (Save)'.
  127.  */
  128. Menu_item SaveFileHandler(item, op)
  129.      Menu_item    item;
  130.      Menu_generate    op;
  131. {
  132.   switch (op) 
  133.   {
  134.    case MENU_DISPLAY:
  135.     break;
  136.    case MENU_DISPLAY_DONE:
  137.     break;
  138.    case MENU_NOTIFY:
  139.     if (strcmp(cdfilename, "untitled") == 0)            /* No filename specified yet */
  140.     {
  141.       Browse(NULL, BrowseSave, 0, "#CD Edit Document#", "cdEdit");
  142.     }
  143.     else 
  144.     {
  145.       Browse(cdfilename, BrowseCheckSave, 0, "#CD Edit Document#", "cdEdit");
  146.     }
  147.     break;
  148.    case MENU_NOTIFY_DONE:
  149.     break;
  150.   }
  151.   return item;
  152. }
  153.  
  154. /*
  155.  * Menu handler for `DocumentMenu (Save as ... )'.
  156.  */
  157. Menu_item SaveAsFileHandler(item, op)
  158.      Menu_item    item;
  159.      Menu_generate    op;
  160. {
  161.   switch (op) 
  162.   {
  163.    case MENU_DISPLAY:
  164.     break;
  165.    case MENU_DISPLAY_DONE:
  166.     break;
  167.    case MENU_NOTIFY:
  168.     Browse(NULL, BrowseSave, 0, "#CD Edit Document#", "cdEdit");
  169.     break;
  170.    case MENU_NOTIFY_DONE:
  171.     break;
  172.   }
  173.   return item;
  174. }
  175.  
  176. /*
  177.  * Menu handler for `DocumentMenu (Close)'.
  178.  */
  179. Menu_item CloseFileHandler(item, op)
  180.      Menu_item    item;
  181.      Menu_generate    op;
  182. {
  183.   switch (op) 
  184.   {
  185.    case MENU_DISPLAY:
  186.     break;
  187.    case MENU_DISPLAY_DONE:
  188.     break;
  189.    case MENU_NOTIFY:
  190.     if (CheckChanges(CloseFile) == NOTICE_YES)                /* Check if unsaved changes exist in edit list */
  191.     {
  192.       return item;
  193.     }
  194.     change = 0;
  195.     editmin = 0;
  196.     editsec = 0;
  197.     editTotalTracks = 0;
  198.     xv_set(cdEdit_EditPopup->DiscLabelText, PANEL_VALUE, "", NULL); /* Clear the text fields in the edit panel */
  199.     xv_set(cdEdit_EditPopup->EditPopupStartTrackText, PANEL_VALUE, "", NULL); 
  200.     xv_set(cdEdit_EditPopup->EditPopupStartMinText, PANEL_VALUE, "", NULL); 
  201.     xv_set(cdEdit_EditPopup->EditPopupStartSecText, PANEL_VALUE, "", NULL); 
  202.     xv_set(cdEdit_EditPopup->EditPopupStartFrameText, PANEL_VALUE, "", NULL); 
  203.     xv_set(cdEdit_EditPopup->EditPopupEndTrackText, PANEL_VALUE, "", NULL); 
  204.     xv_set(cdEdit_EditPopup->EditPopupEndMinText, PANEL_VALUE, "", NULL); 
  205.     xv_set(cdEdit_EditPopup->EditPopupEndSecText, PANEL_VALUE, "", NULL); 
  206.     xv_set(cdEdit_EditPopup->EditPopupEndFrameText, PANEL_VALUE, "", NULL); 
  207.     xv_set(cdEdit_EditPopup->EditPopupLabelText, PANEL_VALUE, "", NULL); 
  208.     xv_set(cdEdit_EditPopup->EditPopupDurationText, PANEL_VALUE, "", NULL); 
  209.     EditDeleteAll(item, NULL);
  210.     strcpy(cdfilename, "untitled");
  211.     change = 0;
  212.     UpdateHeader(0);
  213.     break;
  214.    case MENU_NOTIFY_DONE:
  215.     break;
  216.   }
  217.   return item;
  218. }
  219.  
  220. /*
  221.  * This function will load a file into the edit list, assuming there are no errors in opening the file.
  222.  */
  223. int OpenHandler(filename, id)
  224.      char *filename;
  225.      int id;
  226. {
  227.   FILE *fp;    
  228.   int i, oldlines;
  229.   int dummy;
  230.   char buf[100], header[20], tempLabel[MaxLabelLength + 14];
  231.   char discLabel[MaxLabelLength];
  232.   int tempTotalTracks, tempmin, tempsec;
  233.   
  234.   if (CheckforUntitled (filename) == OK)                /* Check if filename is 'untitled' (unacceptable) */
  235.   {
  236.     AlertMessage("'untitled' is not an acceptable filename.", "please type in a new one.");
  237.     return Error;
  238.   }
  239.   if (CheckChanges (LoadFile) == NOTICE_YES)                /* Check if there are unsaved changes */
  240.   {
  241.     return OK;
  242.   }
  243.   fp = fopen(filename, "r");                
  244.   fgets(header, 20, fp);
  245.   if (strcmp(header, "#CD Edit Document#\n") != 0)            /* Check if the file is a cdEdit file */
  246.   {
  247.     AlertMessage("This is not a cdEdit document.", "Please try another file");
  248.     fclose(fp);
  249.     return Error;
  250.   }
  251.   oldlines = lines;
  252.   fscanf(fp, "#Total number of tracks : %d\n", &tempTotalTracks);
  253.   fscanf(fp, "#Total time : %d %d\n", &tempmin, &tempsec);
  254.   if (discInPlayer == 1 && (tempTotalTracks != toc->size
  255.                 || tempmin != toc->total_msf->min || tempsec != toc->total_msf->sec)) 
  256.   {
  257.     AlertMessage("This document is not compatible with the disc currently in the player.",
  258.          "Please try another document, or change the disc.");
  259.     fclose(fp);
  260.     return Error;
  261.   }
  262.   strcpy(cdfilename, filename);
  263.   editTotalTracks = tempTotalTracks;
  264.   editmin = tempmin;
  265.   editsec = tempsec;
  266.   fgets(tempLabel, MaxLabelLength + 14, fp);
  267.   strcpy(discLabel, &tempLabel[14]);
  268.   discLabel[strlen(discLabel) - 1] = '\0';
  269.   xv_set(cdEdit_EditPopup->DiscLabelText, PANEL_VALUE, discLabel, NULL);
  270.   fscanf(fp, "#Number of edits : %d\n", &lines);                        /* Start loading the edit list from the file */
  271.   for (i=0; i < lines; i++) 
  272.   {
  273.     fscanf(fp, "\n");
  274.     fscanf(fp, "#Edit Number : %d\n", &dummy);
  275.     fscanf(fp, "Start time : %d %d %d %d\n", &starttrack[i], &startmin[i], 
  276.        &startsec[i], &startframe[i]);
  277.     fscanf(fp, "End time : %d%d%d%d\n", &endtrack[i], &endmin[i], 
  278.        &endsec[i], &endframe[i]);
  279.     fscanf(fp, "Volume : %d\n", &volume[i]);
  280.     fscanf(fp, "Balance : %d\n", &balance[i]);
  281.     fscanf(fp, "Duration : %d\n", &duration[i]);
  282.     fgets(tempLabel, MaxLabelLength + 8, fp);
  283.     strcpy(label[i], &tempLabel[8]);
  284.     label[i][strlen(label[i]) - 1] = '\0';
  285.   }
  286.   fclose (fp);
  287.   if (editnum != -1)                            /* Deselect any currently selected entry */
  288.   {
  289.     xv_set(cdEdit_EditPopup->EditList,
  290.        PANEL_LIST_SELECT, editnum, FALSE,
  291.        NULL);
  292.   }
  293.   for (i=0; i<oldlines; i++)                        /* Replace the old strings by the new ones */
  294.   {
  295.     PrintOnList(i, buf);
  296.     xv_set (cdEdit_EditPopup->EditList,
  297.         PANEL_LIST_STRING, i, buf,
  298.         PANEL_LIST_FONT, i, font,
  299.         NULL);
  300.   }
  301.   if (oldlines < lines)                            /* Insert the additional new strings */
  302.   {
  303.     for (i=oldlines; i<lines; i++) 
  304.     {
  305.       PrintOnList(i, buf);
  306.       xv_set (cdEdit_EditPopup->EditList,
  307.           PANEL_LIST_INSERT, i,
  308.           PANEL_LIST_STRING, i, buf,
  309.           PANEL_LIST_FONT, i, font,
  310.           NULL);
  311.     }
  312.   }
  313.   else                                    /* Delete the additional old entries */
  314.   {
  315.     for (i=lines; i< oldlines; i++)
  316.     {
  317.       xv_set(cdEdit_EditPopup->EditList,
  318.          PANEL_LIST_DELETE, lines,
  319.          NULL);
  320.     }
  321.   }
  322.   PrintNumEdit();                            /* Update the edit status information */
  323.   xv_set(cdEdit_EditPopup->CurrentSelectionMsg, PANEL_LABEL_STRING, "Current Selection : None", NULL); /*  */
  324.   editnum = -1;
  325.   change = 0;
  326.   UpdateHeader(0);
  327.   xv_set(cdEdit_EditPopup->ModifyButton, PANEL_INACTIVE, TRUE, NULL); /* Set the modify, delete buttons to inactive */
  328.   xv_set(cdEdit_EditPopup->DeleteButton, PANEL_INACTIVE, TRUE, NULL);
  329.   return OK;
  330. }
  331.  
  332. /*
  333.  * Notify callback function for `SaveButton'.
  334.  * This function will save the current edit list onto the file as specified in the filename textfield.  If the file already exists, the user is
  335.  * warned and given the choice to proceed or abort.
  336.  */
  337. int SaveHandler(filename, id)
  338.      char *filename;
  339.      int id;
  340. {
  341.   FILE *fp;            
  342.   struct stat stbuf;
  343.   int i, result;
  344.   char header[20], buf[40];
  345.   char discLabel[MaxLabelLength];
  346.  
  347.   if (CheckforUntitled(filename) == OK)                    /* Check if filename is 'untitled' (unacceptable) */
  348.   {
  349.     AlertMessage("'untitled' is not an acceptable filename.",
  350.          "please type in a new one.");
  351.     return Error;
  352.   }
  353.   if (stat(filename, &stbuf) == 0)                    /* Check if the file already exists */
  354.   {
  355.     fp = fopen(filename, "r");
  356.     fgets(header, 20, fp);
  357.     fclose(fp);
  358.     if (strcmp(header, "#CD Edit Document#\n") != 0)            /* Check if it is a cdEdit file */
  359.     {
  360.       sprintf(buf, "This file is not a CD edit document.");
  361.     }
  362.     else 
  363.     {
  364.       sprintf(buf, "This CD edit document exists.");
  365.     }
  366.     result = notice_prompt(cdEdit_EditPopup->EditPopup, NULL,
  367.                NOTICE_MESSAGE_STRINGS,
  368.                buf,
  369.                "Do you wish to overwrite it?",
  370.                NULL,
  371.                NOTICE_BUTTON_NO,    "Yes",
  372.                NOTICE_BUTTON_YES,    "No",
  373.                NULL);
  374.     if (result == NOTICE_YES)                        /* Abort the save operation */
  375.     {
  376.       return Error;
  377.     }
  378.   }                                    /* end if (stat... */
  379.   fp = fopen(filename, "w");
  380.   if (fp == (FILE*)NULL)                        /* Was there an error in opening the file for writing? */
  381.   {                                    /* Yes, return without doing anything else */
  382.     return Error;
  383.   }
  384.   lines = xv_get(cdEdit_EditPopup->EditList, PANEL_LIST_NROWS);        /* Get the number of lines in the edit list */
  385.   fprintf(fp, "#CD Edit Document#\n");                    /* Start writing the file with info from the edit list */
  386.   if (discInPlayer == 1)
  387.   {
  388.     fprintf(fp, "#Total number of tracks : %d\n", toc->size);
  389.     fprintf(fp, "#Total time : %d %d\n", toc->total_msf->min, toc->total_msf->sec);
  390.   }
  391.   else 
  392.   {
  393.     fprintf(fp, "#Total number of tracks : %d\n", editTotalTracks);
  394.     fprintf(fp, "#Total time : %d %d\n", editmin, editsec);
  395.   }
  396.   strcpy(discLabel, (char *) xv_get(cdEdit_EditPopup->DiscLabelText, PANEL_VALUE));
  397.   fprintf(fp, "#Disc Label : %s\n", discLabel);
  398.   fprintf(fp, "#Number of edits : %d\n", lines);
  399.   for (i=0; i < lines; i++) 
  400.   {
  401.     fprintf(fp, "\n");
  402.     fprintf(fp, "#Edit Number : %d\n", i + 1);
  403.     fprintf(fp, "Start time : %d %d %d %d\n", starttrack[i], startmin[i], startsec[i],
  404.         startframe[i]);
  405.     fprintf(fp, "End time : %d %d %d %d\n", endtrack[i], endmin[i], endsec[i],
  406.         endframe[i]);
  407.     fprintf(fp, "Volume : %d\n", volume[i]);
  408.     fprintf(fp, "Balance : %d\n", balance[i]);
  409.     fprintf(fp, "Duration : %d\n", duration[i]);
  410.     fprintf(fp, "Label : %s\n", label[i]);
  411.   }
  412.   fclose (fp);
  413.   strcpy(cdfilename, filename);
  414.   change = 0;
  415.   UpdateHeader(0);
  416.   return OK;
  417. }
  418.